//
//  YJLiveStateLabel.m
//  CToolBox
//
//  Created by crj on 2017/2/4.
//  Copyright © 2017年 SC. All rights reserved.
//

#import "YJLiveStateLabel.h"
#import "YJProgramBasicModel.h"

#define kLabelHeight    20
#define kLabelFontSize  12
#define kLabelLiveInCircleFontSize 8

@interface YJLiveStateLabel()

@property (nonatomic, assign) YJLiveStateLabelState state;
@property (nonatomic, copy) NSString * liveNameStr;

@property (nonatomic, strong) UIColor *labelForegroundColor;
@property (nonatomic, strong) UIColor *labelBorderColor;
@property (nonatomic, strong) UIColor *labelBackgroundColor;

@end

@implementation YJLiveStateLabel

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setConfig];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setConfig];
    }
    return self;
}

- (void)setConfig{
    
    self.font = [UIFont systemFontOfSize:kLabelFontSize];
    self.textAlignment = NSTextAlignmentCenter;
    self.textColor = [UIColor whiteColor];
    
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = 0.5;
    self.layer.cornerRadius = kLabelHeight/2;
    
    _state = YJLiveStateLabelStateOther;
    _liveNameStr = @"";
    _labelForegroundColor = [UIColor whiteColor];
    _labelBackgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
    _labelBorderColor = [UIColor whiteColor];

}

- (void)reloadLiveState:(YJLiveStateLabelState)state
                liveStr:(NSString *)liveStr
              textColor:(UIColor *)textColor
            borderColor:(UIColor *)borderColor
        backgroundColor:(UIColor *)backgroundColor{

    _state = state;
    _liveNameStr = liveStr;
    _labelForegroundColor = textColor;
    _labelBorderColor = borderColor;
    _labelBackgroundColor = backgroundColor;
    
    self.layer.borderColor = _labelBorderColor.CGColor;
    self.textColor = _labelForegroundColor;
    self.backgroundColor = _labelBackgroundColor;
    self.attributedText = [self convertToAttributedString];

    [self reloadWidth];
    
    if(liveStr.length == 0 || liveStr == nil){
        self.hidden = YES;
    }else{
        self.hidden = NO;
    }
}

- (NSMutableAttributedString *)convertToAttributedString{

    NSMutableAttributedString *attributedString;
    
    if(_state == YJLiveStateLabelStateUnderWay){
        
        
        NSString * livingStr = @"● ";
        
        attributedString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",livingStr,_liveNameStr]];
        [attributedString addAttribute:NSFontAttributeName
                                 value:[UIFont systemFontOfSize:kLabelLiveInCircleFontSize]
                                 range:NSMakeRange(0, 1)];
        [attributedString addAttribute:NSForegroundColorAttributeName
                                 value:_labelForegroundColor
                                 range:NSMakeRange(0, 1)];
        [attributedString addAttribute:NSBaselineOffsetAttributeName value:@(0.36 * (kLabelFontSize - kLabelLiveInCircleFontSize)) range:NSMakeRange(0, 1)];
    }else{
        attributedString = [[NSMutableAttributedString alloc] initWithString:_liveNameStr];
    }
    
    return attributedString;
}

#pragma mark - Private

- (void)reloadWidth{
    
    CGRect rect = [self.attributedText boundingRectWithSize:CGSizeMake(MAXFLOAT, kLabelHeight)
                                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                                       context:nil];
    CGRect frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, rect.size.width+20, kLabelHeight);
    self.frame = frame;
    
}

@end
